<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Models\Wechat;
use App\Models\Domain;
use App\Models\Group;
class Term extends Model
{
	protected $table = "group_template_term";
	public function getConfig($gid,$did,$tid,$company =false)
    {
		$group			= Group::find($gid);
        $term 			= $this->where('id',$tid)->first();
        $isGeneralize 	= Domain::where('id',$did)->value('status');
        $str 			= "<?php \n";
        $str 			.= "//是否打开推广：0-关闭推广 1-打开推广 \n";
        $str 			.= '$isGeneralize='.$isGeneralize."; \n";
        $str 			.= "//跳转类型：0-不跳转 1-手机端 2-全部跳转 \n";
        $str 			.= '$go_to_type='.$group->go_to_type."; \n";
        $str 			.= "//ip限制 \n";
        $term->ips 		= str_replace('*','.',$term->ips);
        $str 			.= '$ips='.$term->ips."; \n";
        $str 			.= "//投放时间段 \n";
        $str 			.= '$delivery_time="'.$term->delivery_time."\"; \n";
        $jumpList 		= Domain::where('gid',$group->target_gid)->where('status',1)->select('domain','directory')->get();
        $str 			.= "//跳转分组列表 \n";
        $str			.= '$jumpList=array(';
        $outJumpList	= "";
        foreach ($jumpList as $keyJL => $objJL){
            if($keyJL > 0) $outJumpList .=",";
            $outJumpList .= '"'.$objJL->domain.'/'.$objJL->directory.  '"';
        }
        $str 			.= $outJumpList;
        $str			.= ");\n";
        $str 			.= "//跳转目标[分组ID] \n";
        $str 			.= '$target_gid='.$group->target_gid."; \n";
        $str 			.= "//渠道限制 null-不限制 \n";
        $str 			.= '$channel="'.trim($term->channel)."\"; \n";
        $str 			.= "//区域过滤：0-关闭，2：开启 \n";
        $str 			.= '$area_config='.$term->area_config."; \n";
        $str 			.= "//设置过滤地区（不投放区域） null-不限制\n";
        $str 			.= '$area_list="'.trim(preg_replace('/\d+\-/','',$term->area_list))."\"; \n";
        $str 			.= "//设备类型：0-全部 ，1-移动，2-PC \n";
        $str 			.= '$device_type='.$term->device_type."; \n";
        $str 			.= "//站点类型 \n";
        $str 			.= '$station_type='.$term->station_type."; \n";
        $str			.= "//Start---配置微信号";
        $str			.= "\n //微信号，微信昵称，默认随机值，随机值剩余次数，客服上线时间，客服下线时间 \n";
        if(empty($group->wechat_ids)){
            $wechatList = Wechat::whereIn('wx_num',explode(',',$group->wechat_list))->where('status',1)
                ->select('wx_num','wx_nike','max_rand','status','online_time','offline_time')
                ->get()->toArray();
        }else{
            $wechatList = Wechat::whereIn('id',explode(',',$group->wechat_ids))->where('status',1)
                ->select('wx_num','wx_nike','max_rand','status','online_time','offline_time')
                ->get()->toArray();
        }
        $str			.= '$wechatList=['."\n";
        if(count($wechatList) != 0){
            foreach ($wechatList as $key => $obj) {
                $obj 	= '["' . str_replace(',', '","', join(',', $obj)) . "\"],\n";
                $str	.= $obj;
            }
        }else{
            $str		.= '["DefautWechat", "LServer", "5", "50", "00:00", "23:59"],';
        }
        $str			.= "]; \n";
        $str			.= "//End---配置微信号 \n";
        $str 			.= "//引入判断文件 \n";
        if($company){
            $str 		.= 'include($_SERVER["DOCUMENT_ROOT"]."/judgeCompany.php")'."; \n";
        }else{
            $str 		.= 'include($_SERVER["DOCUMENT_ROOT"]."/judge.php")'."; \n";
        }
        $str 			.= "?> \n";
        return $str;
    }
}
